//
//  ImageSelectionController_iPad.m
//  Tazama NGO
//
//  Created by Jack Sheriff on 12/6/11.
//  Copyright (c) 2011 The MITRE Corporation. All rights reserved.
//

#import "ImageSelectionController_iPad.h"
#import <MobileCoreServices/UTCoreTypes.h>

@interface ImageSelectionController_iPad ()

@property (nonatomic, retain) UIPopoverController *popoverController;

@end

@implementation ImageSelectionController_iPad

@synthesize delegate;
@synthesize albumButton;
@synthesize cameraButton;
@synthesize imageView;
@synthesize imagePickerController;
@synthesize popoverController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

-(IBAction)cancelPressed:(id)sender
{
	if ([delegate respondsToSelector:@selector(didCancelItem)])
	{
		[delegate didCancelItem];
	}
}

-(IBAction)savePressed:(id)sender
{
    NSLog(@"image size = %f x %f, scale = %f", imageView.image.size.width, imageView.image.size.height, imageView.image.scale);
	if ([delegate respondsToSelector:@selector(didSaveItem)])
	{
		[delegate didSaveItem];
	}
}

- (void)viewDidLoad
{
	if (imagePickerController == nil)
	{
		imagePickerController = [[UIImagePickerController alloc] init];
	}
	imagePickerController.allowsEditing = YES;
	imagePickerController.delegate = self;		
    
    UIBarButtonItem *cancelButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelPressed:)] autorelease];
    self.navigationItem.leftBarButtonItem = cancelButton;
    
	UIBarButtonItem *doneButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(savePressed:)] autorelease];
	self.navigationItem.rightBarButtonItem = doneButton;
	
    [super viewDidLoad];
}

- (IBAction)grabAlbum
{
    if (self.popoverController != nil && [self.popoverController isPopoverVisible]) {
        // Popover displayed and user tapped Album button.  Hide the popover.
        [self.popoverController dismissPopoverAnimated:YES];
    }
    else {
        if (self.popoverController == nil) {
            self.popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
        }
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        [self.popoverController presentPopoverFromBarButtonItem:albumButton permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    }
}

- (IBAction)grabCamera
{
    if (self.popoverController != nil && [self.popoverController isPopoverVisible]) {
        // Popover displayed and user tapped Album button.  Hide the popover.
        [self.popoverController dismissPopoverAnimated:YES];
    }
    else {
        if (self.popoverController == nil) {
            self.popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
        }
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        else
        {
            imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
        [self.popoverController presentPopoverFromBarButtonItem:cameraButton permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self.popoverController dismissPopoverAnimated:YES];
    
	NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if (CFStringCompare((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo)
	{               
        UIImage* image = [info objectForKey:UIImagePickerControllerEditedImage];
        if (!image)
		{
			image = [info objectForKey:UIImagePickerControllerOriginalImage];             
		}
		[image retain];
		imageView.image = image;
		NSLog(@"image size = %f x %f, scale = %f", imageView.image.size.width, imageView.image.size.height, imageView.image.scale);
	}
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
	[picker dismissModalViewControllerAnimated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Support all orientations
	return YES;
}

@end
